
from pyecharts import options as opts
from pyecharts.charts import Tree
# from pycharts
import pymongo
from bson.json_util import dumps
import json
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["spider-db"]
collection = db["wzj_category"]
result = collection.find({})  # bjson格式的数据
json_str = dumps(result)  # 将数据库查询结果转为json字符串
json_obj = json.loads(json_str)  # 将json字符串转为python的数据对象

data = [
    {
        "name": "筛选分类",
        "children": json_obj,
    }
]

# 定义图表对象
tree = (
    Tree()
    .add("", data)
)

# 全局配置（所有图表通用配置）
tree.set_global_opts(
    title_opts=opts.TitleOpts(
        title="家具分类数据可视化",
        pos_top='1%',
        pos_left='center'
    ),
    datazoom_opts= opts.DataZoomOpts(
        type_="slider",
        is_show=True
    ),
    toolbox_opts=opts.ToolboxOpts(
        is_show=True,
    )
)

# 当前图表类型的特有配置（例如：tree只能用tree下面提供的配置）
tree.set_series_opts(
    label_opts=opts.LabelOpts(
        position='right',
        color='red'
    ),
    layout="radial",
    itemstyle_opts=opts.ItemStyleOpts(
        color='purple',
    ),
    linestyle_opts=opts.LineStyleOpts(
        color='blue'
    )

)

tree.height = '90vh'
tree.width = '90vw'

# 渲染图表
tree.render("tree_category_options.html")  # 生成一个展示可视化图表的html
